# -*- coding: utf-8 -*- 
import cx_Oracle
from django.db import models
 
class ref_operations:
    list_op = None;
    rop_id = None
    name  = None
#     trace = None

    def __init__(self, connect):
        self.cursor = connect.cursor() 
        self.time=self.cursor.var(cx_Oracle.UNICODE)
        self.time2=self.cursor.var(cx_Oracle.NUMBER)
        self.cursor.execute("""
            select msg_id
                  ,name
              from messages 
             where msg_id = 1
            """)            
        self.l_cursor = self.cursor.fetchone()
           # self.rop_id = l_cursor.rop_id
           # print(l_cursor[1].name)
#        print(self.l_cursor)
        self.rop_id  = self.l_cursor[0]        
        self.name = self.l_cursor[1]
#        self.name  = self.l_cursor[2]        
        #self.rop_id = self.time2.getvalue()
        #self.name = self.time.getvalue()
        for row in self.cursor:
            list_op[0] = [row[0], row[1]]

def test_log(connect, msg):
    cursor = connect.cursor() 
    mm = self.cursor.var(cx_Oracle.STRING)
    mm = msg
    print(mm)
    cursor.execute(u"""begin test_log(:log); end;""", log = mm)

def get_top_sql(connect):
    cursor = connect.cursor() 
    cursor.execute("""
        SELECT *
          FROM (SELECT t.sql_text,
               t.sql_id,
               cpu_time,
               elapsed_time,
               buffer_gets,
               executions,
               buffer_gets / executions be,
               fetches,
               t.rows_processed,
               t.disk_reads,
               t.hash_value,
               t.LAST_LOAD_TIME,
               t.LAST_ACTIVE_TIME,
               t.ADDRESS,
               t.OLD_HASH_VALUE,
               t.PLAN_HASH_VALUE
          FROM v$sql t
         WHERE executions != 0
         ORDER BY t.cpu_time DESC)
         WHERE rownum <= 30
        """)
#    for cur in cursor:
#        print(cur)
    return cursor.fetchall()

class note(models.Model):
    note_id = models.CharField(max_length=13, primary_key=True)
    name = models.CharField(max_length=255)
    pub_date = models.DateTimeField("data published")
    